<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="/js/jquery-1.11.0.min.js"></script>
</head>

<body>
    <input type="file" id="file" /> <button id="btnUpload">上传文件</button>
    <br />
    <img src="/image/loading.gif" alt="" style="display: none;" id="img1">
    <img src="" alt="" id="img" width="500px" />
    <script>
        $(function () {
            // 监听整个文档下的所有ajax的开始请求
            $(document).ajaxStart(function () {
                $('#img1').show()
            })
            // 监听整个文档下的所有ajax的结束 注意这两个事件只可以绑定document 不可绑定dom元素
            $(document).ajaxStop(function () {
                $('#img1').hide()
            })

            $('#btnUpload').on('click', function () {
                var files = $('#file')[0].files
                var fd = new FormData()
                fd.append('avatar', files[0])
                $.ajax({
                    method: 'POST',
                    url: 'http://www.liulongbin.top:3006/api/upload/avatar',
                    data: fd,
                    processData: false,
                    contentType: false,
                    success: function (res) {
                        console.log(res);
                        if (res.status === 200) {
                            $('#img').attr('src', 'http://www.liulongbin.top:3006' + res.url)
                        }
                    }
                })
            })

          

        })

    </script>
</body>

</html>